package juhouse.biz.kc.action;
import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import juhouse.biz.kc.dao.DBDao;
import juhouse.biz.kc.dao.FormDao;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;


public class FormAction  extends DispatchAction{
	
	public StringBuffer getSelectOptions(String listId,String listValue)
	{	
		FormDao dao=new FormDao();
		StringBuffer outBuf=dao.getSelectOptions(listId,listValue);
		return outBuf;
	}
	
	public StringBuffer getSelectOptionNames(String listId,String listValue)
	{	
		FormDao dao=new FormDao();
		StringBuffer outBuf=dao.getSelectOptionNames(listId,listValue);
		return outBuf;
	}

	
	public StringBuffer getBookCondi(HttpServletRequest request)
	{	
		
		String bookId=request.getParameter("bookId");	    	
		FormDao dao=new FormDao();
		StringBuffer strlist=dao.getBookCondi(bookId,request);
		return strlist;
	}

	private String[] getFieldNames(String strTableName,HttpServletRequest request)
	{

		int iFieldCount=0;
		String sFlag="DBFIELD_"+strTableName+"_";
		Enumeration  paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) { 
		  String paramName = (String)paramNames.nextElement(); 
		  if (paramName.startsWith(sFlag))
			  iFieldCount++;
		} 
		
		String[] fieldNames = new String[iFieldCount];
		
		int i=0;
		Enumeration  paramNames1 = request.getParameterNames();
		while (paramNames1.hasMoreElements()) { 
		  String paramName = (String)paramNames1.nextElement(); 
		  if (paramName.startsWith(sFlag))
		  {
			  fieldNames[i++]=paramName.substring(sFlag.length());
		  }
		} 
		return fieldNames;
	}
	
	
	private String[][] getFieldValues(String[] fieldNames,String strTableName,HttpServletRequest request)
	{
		String[][] fieldValues=new String[1][fieldNames.length];
		String sFlag="DBFIELD_"+strTableName+"_";
		
		for (int i=0;i<fieldNames.length;i++)
		{
			String sFieldName=sFlag+fieldNames[i];
			String[] paramValues = request.getParameterValues(sFieldName);
			fieldValues[0][i]=paramValues[0];
		}
		return fieldValues;
	}	
	
	
	
	
	public ActionForward saveForm(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String[] tableNames = request.getParameterValues("TABLENAME");
		String strTableName=null;
		String strKeyField=null;
		String strKeyValue=null;
		String[] fieldNames=null;
		
		FormDao dao=new FormDao();

		
		for (int i=0;i<tableNames.length;i++)
		{
			strTableName=tableNames[i];
			strKeyField = request.getParameter("DBKEY_"+strTableName);
			if (strKeyField==null) strKeyField="";
			strKeyValue = request.getParameter("DBFIELD_"+strTableName+"_"+strKeyField);
			if (strKeyValue==null) strKeyValue="";
			//System.out.println("====================="+strTableName+"==================");
			fieldNames =getFieldNames(strTableName,request);
			/*
			for (int j=0;j<fieldNames.length;j++)
			{
				System.out.println(fieldNames[j]);
			}
			*/
			String[][] fieldValues=null;
			if (fieldNames.length>0)
			{	
			  fieldValues=getFieldValues(fieldNames,strTableName,request);
			  /*
			  for(int m=0;m<fieldValues.length;m++)
			    for(int n=0;n<fieldValues[m].length;n++)
			    	System.out.println(fieldValues[m][n]);
			   */
			}
			dao.saveForm(strTableName, strKeyField, strKeyValue, fieldNames, fieldValues);
		}

		this.sendRedirect(request, response);
		return null;
	}
	
	
	
	public ActionForward deleteBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
			
		if(request.getSession().getAttribute("manageId")!=null)
		{	
	    	DBDao dao = new DBDao();
			String keyValue = request.getParameter("keyValue");
			String bookId=request.getParameter("bookId");
	        dao.exeSQL("exec NH_Function_BillRevoke 'delete',"+bookId+",'"+keyValue+"'");
	        this.sendRedirect(request, response);
		}
		return null;
	}
	
	
	public ActionForward checkBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {		
		FormDao dao=new FormDao();
		String keyValue = request.getParameter("keyValue");
		String bookId=request.getParameter("bookId");
		String manageId=(String) request.getSession().getAttribute("manageId");
		String manageName=(String) request.getSession().getAttribute("manageName");

		String sResult ="";
		sResult=dao.checkBill(bookId, keyValue, manageId, manageName);
        request.setAttribute("validateResult", sResult);
        System.out.println(sResult);
		return mapping.findForward("validateResult");
	}
	
	
	
	public ActionForward unCheckBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		FormDao dao=new FormDao();
		String keyValue = request.getParameter("keyValue");
		String bookId=request.getParameter("bookId");
		String manageId=(String) request.getSession().getAttribute("manageId");
		String manageName=(String) request.getSession().getAttribute("manageName");

		String sResult ="";
		sResult=dao.unCheckBill(bookId, keyValue, manageId, manageName);
        request.setAttribute("validateResult", sResult);
        System.out.println(sResult);
		return mapping.findForward("validateResult");
	}
	
	
    public void sendRedirect(HttpServletRequest request,HttpServletResponse response) throws IOException{
    	
    	String url = request.getParameter("url");
    	System.out.println(url);
        String pageno=null;
        pageno=request.getParameter("pageno");
        	url=url+"?pageno="+pageno;
        String nowLineNum = request.getParameter("line");
        if (nowLineNum==null) nowLineNum="0";
        	url=url+"&line="+nowLineNum;
        String bookId=request.getParameter("bookId");
        if (bookId!=null) url=url+"&bookId="+bookId;
	    response.sendRedirect(url);	    
    }
}
